In [1]:
%matplotlib inline
from science import *

Make up some data


In [2]:
data=randn(1000)+40

In [3]:
hist(data)


Out[3]:
(array([   3.,   12.,   54.,  124.,  245.,  274.,  193.,   66.,   24.,    5.]),
 array([ 36.3250245 ,  37.02624324,  37.72746197,  38.4286807 ,
         39.12989944,  39.83111817,  40.53233691,  41.23355564,
         41.93477438,  42.63599311,  43.33721185]),
 <a list of 10 Patch objects>)

Make it x-y data


In [4]:
x=arange(len(data))
y=data
plot(x,y,'o')


Out[4]:
[<matplotlib.lines.Line2D at 0x10b239ed0>]

Constant Model

Define Model


In [5]:
def constant(x,a):
    return a

model=MCMCModel(x,y,constant,
            a=Uniform(0,100),
            )
model.run_mcmc(500)
model.plot_chains()


Sampling Prior...
Done.
0.25 s
Running MCMC...
Done.
1.48 s
<matplotlib.figure.Figure at 0x10b0cc590>

In [6]:
model.plot_distributions()



In [7]:
model.triangle_plot()


Replot the data with the fit


In [8]:
plot(x,y,'o')

xfit=linspace(0,1000,200)
yfit=model.predict(xfit)

plot(xfit,yfit,'-')


Out[8]:
[<matplotlib.lines.Line2D at 0x10bf21790>]

Linear Model

Define Model


In [9]:
def linear(x,a,b):
    return a*x+b

model=MCMCModel(x,y,linear,
                a=Uniform(-10,10),
                b=Uniform(0,100),
                )

model.run_mcmc(500)
model.plot_chains()


Sampling Prior...
Done.
0.28 s
Running MCMC...
Done.
1.86 s
<matplotlib.figure.Figure at 0x10ca98510>

chains look weird? run for some more...


In [10]:
model.run_mcmc(500)
model.plot_chains()


Running MCMC...
Done.
1.87 s
<matplotlib.figure.Figure at 0x10bfc3210>

In [11]:
model.plot_distributions()



In [12]:
model.triangle_plot()


Replot the data with the fit


In [13]:
plot(x,y,'o')

xfit=linspace(0,1000,200)
yfit=model.predict(xfit)

plot(xfit,yfit,'-')


Out[13]:
[<matplotlib.lines.Line2D at 0x10df94910>]

Using some real data


In [14]:
xls = pandas.ExcelFile('temperatures.xls')

print xls.sheet_names

data=xls.parse('Sheet 1')
data


[u'Sheet 1']
Out[14]:
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec J-D D-N DJF MAM JJA SON Year.1
0 1880 -43 -27 -26 -37 -32 -39 -24 -14 -30 -24 -20 -23 -28 -999 -999 -31 -26 -25 1880
1 1881 -20 -24 -1 -5 -6 -32 -14 -12 -26 -29 -38 -30 -20 -19 -22 -4 -19 -31 1881
2 1882 -13 -2 -10 -29 -25 -33 -27 -14 -25 -38 -37 -52 -26 -24 -15 -21 -25 -33 1882
3 1883 -46 -41 -18 -22 -27 -11 -10 -21 -31 -34 -33 -29 -27 -29 -47 -23 -14 -33 1883
4 1884 -27 -20 -35 -39 -37 -36 -28 -19 -31 -34 -36 -32 -31 -31 -25 -37 -28 -34 1884
5 1885 -61 -33 -24 -40 -33 -42 -30 -26 -23 -24 -19 -6 -30 -32 -42 -32 -33 -22 1885
6 1886 -37 -44 -28 -21 -19 -33 -8 -23 -20 -31 -34 -30 -27 -25 -29 -23 -21 -28 1886
7 1887 -64 -51 -35 -42 -29 -24 -8 -27 -25 -38 -32 -40 -35 -34 -48 -35 -20 -32 1887
8 1888 -48 -47 -46 -36 -29 -25 -19 -23 -19 -11 -5 -22 -28 -29 -45 -37 -22 -12 1888
9 1889 -23 8 -1 -3 -7 -13 -17 -25 -22 -30 -37 -34 -17 -16 -12 -4 -18 -30 1889
10 1890 -52 -41 -34 -37 -50 -39 -31 -38 -42 -24 -51 -35 -40 -39 -42 -41 -36 -39 1890
11 1891 -49 -54 -18 -31 -21 -24 -23 -20 -17 -24 -39 -10 -28 -30 -46 -23 -22 -27 1891
12 1892 -39 -12 -36 -44 -33 -23 -32 -29 -22 -29 -52 -48 -33 -30 -20 -38 -28 -35 1892
13 1893 -86 -60 -20 -37 -40 -29 -11 -27 -26 -18 -19 -35 -34 -35 -65 -32 -23 -21 1893
14 1894 -52 -33 -26 -45 -40 -44 -21 -26 -36 -29 -39 -30 -35 -36 -40 -37 -31 -35 1894
15 1895 -57 -50 -32 -28 -31 -23 -21 -19 -13 -17 -14 -21 -27 -28 -45 -30 -21 -15 1895
16 1896 -27 -20 -32 -38 -17 -15 -8 -14 -11 -2 -15 -13 -18 -18 -23 -29 -12 -9 1896
17 1897 -22 -20 -19 -9 -5 -14 -5 -10 -15 -12 -19 -17 -14 -14 -18 -11 -10 -15 1897
18 1898 -7 -26 -49 -25 -36 -18 -21 -22 -21 -29 -40 -28 -27 -26 -17 -37 -21 -30 1898
19 1899 -22 -38 -29 -16 -17 -28 -10 -7 -7 -2 13 -27 -16 -16 -29 -21 -15 1 1899
20 1900 -35 5 -1 -11 -8 -10 -10 -11 -6 1 -13 -8 -9 -11 -19 -7 -10 -6 1900
21 1901 -21 0 3 -3 -15 -14 -17 -19 -22 -26 -24 -26 -15 -14 -10 -5 -17 -24 1901
22 1902 -14 5 -23 -26 -29 -30 -17 -27 -26 -32 -42 -46 -25 -24 -12 -26 -25 -33 1902
23 1903 -25 5 -16 -36 -32 -42 -28 -38 -43 -41 -32 -45 -31 -31 -22 -28 -36 -39 1903
24 1904 -56 -45 -34 -43 -42 -35 -33 -34 -38 -31 -12 -21 -35 -37 -49 -40 -34 -27 1904
25 1905 -31 -55 -21 -35 -28 -26 -22 -17 -17 -26 -10 -21 -26 -26 -36 -28 -22 -17 1905
26 1906 -27 -30 -17 -3 -20 -13 -22 -12 -22 -15 -38 -14 -19 -20 -26 -13 -16 -25 1906
27 1907 -42 -50 -28 -46 -50 -45 -39 -40 -28 -25 -44 -43 -40 -37 -35 -41 -41 -33 1907
28 1908 -41 -27 -47 -41 -34 -26 -24 -34 -22 -30 -40 -40 -34 -34 -37 -41 -28 -31 1908
29 1909 -56 -38 -43 -45 -42 -41 -33 -20 -23 -22 -19 -40 -35 -35 -45 -43 -32 -21 1909
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
102 1982 1 9 -10 -1 12 0 12 -3 2 3 4 36 5 5 13 0 3 3 1982
103 1983 47 36 37 27 31 16 11 29 36 10 23 11 26 28 39 32 19 23 1983
104 1984 23 9 24 4 30 -1 12 11 17 4 -5 -14 10 12 14 20 7 5 1984
105 1985 16 -11 11 4 6 13 -5 10 6 5 -1 9 5 3 -3 7 6 3 1985
106 1986 22 37 25 19 17 6 6 7 -1 6 2 7 13 13 23 20 7 2 1986
107 1987 27 39 12 18 17 33 41 19 31 28 23 45 28 24 24 16 31 27 1987
108 1988 53 37 47 36 39 40 28 32 32 30 0 22 33 35 45 41 33 21 1988
109 1989 6 30 29 20 7 8 30 26 32 27 9 30 21 20 19 19 21 23 1989
110 1990 31 30 67 46 37 28 43 24 17 38 37 34 36 36 30 50 32 31 1990
111 1991 34 45 29 44 31 49 47 38 40 20 19 25 35 36 38 34 45 26 1991
112 1992 38 34 38 15 23 16 1 0 -12 -5 -8 10 13 14 32 26 6 -8 1992
113 1993 28 29 28 16 16 9 12 2 -1 13 -1 12 14 13 22 20 8 4 1993
114 1994 22 -7 19 30 16 34 22 15 31 35 38 28 24 22 9 22 23 35 1994
115 1995 43 71 41 41 20 36 49 39 23 44 38 24 39 39 48 34 41 35 1995
116 1996 24 48 31 26 20 18 35 47 24 15 34 33 30 29 32 26 34 25 1996
117 1997 27 32 48 33 31 52 27 34 43 52 57 56 41 39 31 37 38 51 1997
118 1998 55 83 59 56 66 71 65 63 42 40 45 51 58 58 65 60 66 42 1998
119 1999 42 60 27 25 22 35 30 28 30 32 29 34 33 34 51 24 31 31 1999
120 2000 18 52 50 52 30 37 36 39 35 19 28 24 35 36 35 44 37 28 2000
121 2001 38 41 54 42 52 44 52 46 49 46 65 51 48 46 34 49 48 53 2001
122 2002 70 70 88 55 56 47 56 45 52 50 50 37 56 58 63 66 50 51 2002
123 2003 67 50 52 48 53 41 49 63 60 67 49 68 56 53 51 51 51 59 2003
124 2004 53 67 58 52 35 34 19 42 47 60 66 46 48 50 63 48 32 58 2004
125 2005 69 55 68 62 55 57 54 57 67 72 64 63 62 61 57 62 56 68 2005
126 2006 48 63 58 45 41 54 42 63 54 60 64 72 55 55 58 48 53 59 2006
127 2007 88 63 65 67 61 53 56 56 51 54 48 39 58 61 74 64 55 51 2007
128 2008 15 25 65 43 41 34 54 36 53 56 58 48 44 43 27 50 41 56 2008
129 2009 54 46 47 48 55 61 65 55 65 60 67 60 57 56 49 50 60 64 2009
130 2010 69 74 85 76 65 56 49 53 53 63 71 43 63 65 68 76 53 63 2010
131 2011 44 43 56 55 42 50 65 65 50 54 48 45 52 51 43 51 60 51 2011

132 rows × 20 columns


In [15]:
x=data['Year']
y=data['J-D']/100.0
plot(x,y,'-o')
xlabel('Year')
ylabel('Temperature Deviation')


Out[15]:
<matplotlib.text.Text at 0x10b03f5d0>

In [16]:
def linear(x,m,b):
    return m*x+b

x=data['Year']-1880
y=data['J-D']/100.0

model=MCMCModel(x,y,linear,
                m=Uniform(-10,10),
                b=Uniform(-100,100),
                )

model.run_mcmc(500)
model.plot_chains()


Sampling Prior...
Done.
0.30 s
Running MCMC...
Done.
18.79 s
<matplotlib.figure.Figure at 0x10b7f4150>

In [17]:
model.plot_distributions()



In [18]:
model.triangle_plot()



In [19]:
plot(x+1880,y,'-o')

xfit=linspace(1870,2020,200)
yfit=model.predict(xfit-1880)

plot(xfit,yfit,'-')


Out[19]:
[<matplotlib.lines.Line2D at 0x10d58e990>]

Sampling


In [20]:
plot(x,y,'o')

xfit=linspace(1850,2050,200)
model.plot_predictions(xfit-1880,100)


Quadratic?


In [21]:
def quadratic(x,a,b,c):
    return a*x**2 + b*x + c

make the data a bit more numerically stable


In [22]:
x=data['Year']
y=data['J-D']/100.0

# make a little more palatable
x=x-mean(x)
y=y-mean(y)

model=MCMCModel(x,y,quadratic,
                a=Uniform(-10,10),
                b=Uniform(-10,10),
                c=Uniform(-100,100),
                )

model.run_mcmc(500)
model.plot_chains()


Sampling Prior...
Done.
0.33 s
Running MCMC...
Done.
27.41 s
<matplotlib.figure.Figure at 0x10c62eb10>

In [23]:
model.plot_distributions()
model.triangle_plot()

figure()
plot(x,y,'o')

xfit=linspace(min(x)-50,max(x)+50,200)
model.plot_predictions(xfit,100)


what is I used the raw data?


In [24]:
x=data['Year']
y=data['J-D']/100.0

model=MCMCModel(x,y,quadratic,
                a=Uniform(-10,10),
                b=Uniform(-1000,1000),
                c=Uniform(-2000,2000),
                )

model.run_mcmc(500)
model.plot_chains()


Sampling Prior...
Done.
0.35 s
Running MCMC...
Done.
29.40 s
<matplotlib.figure.Figure at 0x111c41850>

In [25]:
model.run_mcmc(500)
model.plot_chains()


Running MCMC...
Done.
29.02 s
<matplotlib.figure.Figure at 0x10bcb60d0>

In [26]:
model.plot_distributions()
model.triangle_plot()

figure()
plot(x,y,'o')

xfit=linspace(min(x)-50,max(x)+50,200)
model.plot_predictions(xfit,100)


Logistic Model

http://mathinsight.org/bacteria_growth_logistic_model

definition of function from http://en.wikipedia.org/wiki/Logistic_function:

\begin{equation} f(x)=\frac{L}{1+e^{-k(x-x_o)}} \end{equation}

In [27]:
def logistic(x,L,k,xo):
    return L/(1.0+exp(-k*(x-xo)))

In [28]:
datastr="""0	0	0.022
16	1	0.036
32	2	0.060
48	3	0.101
64	4	0.169
80	5	0.266
96	6	0.360
112	7	0.510
128	8	0.704
144	9	0.827
160	10	0.928
"""

x,y=zip(*[(float(line.split()[0]),float(line.split()[2])) for line in datastr.split('\n') if line])
plot(x,y,'o')


Out[28]:
[<matplotlib.lines.Line2D at 0x10de67450>]

In [29]:
model=MCMCModel(x,y,logistic,
                L=Uniform(0,5),
                k=Uniform(0,20),
                xo=Uniform(0,1000),
                )
model.run_mcmc(500)
model.plot_chains()


Sampling Prior...
Done.
0.35 s
Running MCMC...
Done.
1.46 s
<matplotlib.figure.Figure at 0x10de8aad0>

In [30]:
model.run_mcmc(500)
model.plot_chains()


Running MCMC...
Done.
1.38 s
<matplotlib.figure.Figure at 0x10bce2bd0>

In [31]:
model.plot_distributions()
model.triangle_plot()

figure()
plot(x,y,'o')

xfit=linspace(min(x),max(x)+50,200)
model.plot_predictions(xfit,100)



In [ ]:


In [ ]: